package com.kingsystem.kingteachermarks_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingsystem.kingteachermarks_backend.entity.SystemFunction;
import com.kingsystem.kingteachermarks_backend.entity.SystemUser;
import com.kingsystem.kingteachermarks_backend.mapper.SystemFunctionMapper;
import com.kingsystem.kingteachermarks_backend.mapper.SystemUserMapper;
import com.kingsystem.kingteachermarks_backend.service.impl.SystemUserServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author leiyue_yao
 * @since 2025-05-28
 */
@Controller
@RequestMapping("/systemFunction")
@CrossOrigin
public class SystemFunctionController {
    @Autowired
    private SystemFunctionMapper sfm;

    @Autowired
    private SystemUserServiceImpl sfi;

    @RequestMapping("/getAllMenuFunctions")
    @ResponseBody
    public List<SystemFunction> getAllMenuFunctions(){
        QueryWrapper<SystemFunction> qw = new QueryWrapper<>();
        qw.eq("function_type","menu");
        List<SystemFunction> li = sfm.selectList(qw);
        return li;
    }

    @PostMapping("/getfounctionbyid")
    @ResponseBody
    public List<SystemFunction> getfounctionbyid(
            @RequestParam(required = false) String parentFunctionId,
            @RequestParam(required = false) String functionName) {

        QueryWrapper<SystemFunction> q = new QueryWrapper<>();

        if (parentFunctionId != null && !parentFunctionId.isEmpty()) {
            q.eq("parent_function_id", parentFunctionId);
        }

        if (functionName != null && !functionName.isEmpty()) {
            q.like("function_name", functionName);
        }

        return sfm.selectList(q);
    }

    @PostMapping("/delDeptById")
    @ResponseBody
    public String delDeptById(@RequestBody SystemFunction record){
        try {
            QueryWrapper<SystemFunction> qw = new QueryWrapper<>();
            qw.eq("tableKey", record.getTableKey());
            sfm.delete(qw);
            return "success";
        }
        catch(Exception e){
            return e.getMessage();
        }
    }


    @RequestMapping("/addfunction")
    @ResponseBody
    public String adduser(@RequestBody SystemFunction payload) {
        try{
            sfm.insert(payload);
            return "success";
        }
        catch(Exception e){
            return e.getMessage();
        }
    }


    @PostMapping("/editfounction")
    @ResponseBody
    public String editDept(@RequestBody SystemFunction params) {
        try {
            System.out.println(params);
            sfm.updateById(params);

            return "success";
        } catch (Exception e) {
            return "error: " + e.getMessage();
        }
    }

    @PostMapping("/getPagedFunction")
    @ResponseBody
    public List<SystemFunction> getPagedFunction(){
        Page<SystemFunction> p = new Page<>(2, 5);
        QueryWrapper<SystemFunction> qw = new QueryWrapper<>();
        qw.orderByAsc("TableKey");
        Page<SystemFunction> reslutPage =sfm.selectPage(p,qw);

        List<SystemFunction> li = reslutPage.getRecords();
        return li;
    }
}
